package com.meida.module.bus.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.admin.client.entity.BaseUser;
import com.meida.module.bus.client.entity.*;
import com.meida.module.bus.provider.mapper.BusPurchaseDetailsMapper;
import com.meida.module.bus.provider.service.BusPurchaseDetailsService;
import com.meida.module.bus.provider.service.BusSupplierService;
import com.meida.module.system.provider.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购明细表接口实现类
 *
 * @author flyme
 * @date 2020-10-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusPurchaseDetailsServiceImpl extends BaseServiceImpl<BusPurchaseDetailsMapper, BusPurchaseDetails> implements BusPurchaseDetailsService {

    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private BusSupplierService busSupplierService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPurchaseDetails bpd, EntityMap extra) {
        return ResultBody.ok();
    }



    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPurchaseDetails> cq, BusPurchaseDetails bpd, EntityMap requestMap) {

        cq.createJoin(BusPurchase.class).setMainField("purchaseId").setJoinField("purchaseId");
        if(FlymeUtils.isNotEmpty(requestMap.get("versionType"))&&requestMap.get("versionType").equals("1")){
            cq.select("bmplan.materialPlanId,bmplan.parentId,bmplan.projectId,bmplan.versionId,bmplan.brandName,bmplan.packageNo," +
                "bmplan.packageName,bmplan.majorCode,bmplan.subjectName,bmplan.materialWbsCode,bmplan.materialCode,bmplan.materialDesc," +
                "bmplan.levelNo,bmplan.contractDesc,bmplan.materialArea,bmplan.materialUse,bmplan.materiaSpecs,bmplan.technicalData,bmplan.materialName,bmplan.costQuantity100,bmplan.costQuantity200,bmplan.measurementUnit,bmplan.loss," +
                "bmplan.majorName,bmplan.planPurchaseCount,bmplan.endPurchaseCount,bmplan.ztPurchaseCount,bmplan.dhPurchaseCount,bmplan.purchaseCount,bmplan.receivedDate,bmplan.orderflg,bmplan.status,bmplan.applayStatus,bmplan.planType," +
                "bmplan.vendorName, bmplan.caseType,project.projectName,project.projectCode,user.userName,bpd.purchaseDetailsId");
            cq.createJoin(BusMaterialPlan100.class).setMainAlias("bmplan").setMainField("materialId").setMainAlias("bpd").setJoinField("materialPlanId");
//            cq.createJoin(BusSupplier.class).setMainAlias("supplier").setMainField("vendorId").setMainAlias("bpd").setJoinField("supplierId");
//            cq.createJoin(SysDictData.class).setMainAlias("dictdata").setMainField("documentType").setMainAlias("bpd").setJoinField("dicDataValue");
//            cq.createJoin(BusMaterial.class).setMainAlias("material").setMainField("materialId").setMainAlias("bpd").setJoinField("materialId");
        }
        if(FlymeUtils.isNotEmpty(requestMap.get("versionType"))&&requestMap.get("versionType").equals("2")){
            cq.select("bmep.materialPlanId,bmep.parentId,bmep.projectId,bmep.versionId,bmep.brandName,bmep.packageNo," +
                "bmep.packageName,bmep.majorCode,bmep.subjectName,bmep.materialWbsCode,bmep.materialCode,bmep.materialDesc, bmep.levelNo,bmep.contractDesc,bmep.materialArea,bmep.materialUse," +
                "bmep.materiaSpecs,bmep.technicalData,bmep.materialName,bmep.costQuantity100,bmep.costQuantity200,bmep.measurementUnit,bmep.loss,bmep.majorName,bmep.planPurchaseCount," +
                "bmep.endPurchaseCount,bmep.ztPurchaseCount,bmep.dhPurchaseCount,bmep.purchaseCount,bmep.receivedDate,bmep.orderflg,bmep.status,bmep.applayStatus,bmep.planType," +
                "bmep.vendorName,bmep.caseType,project.projectName,project.projectCode,user.userName,bpd.purchaseDetailsId");
            cq.createJoin(BusMaterialPlan200.class).setMainAlias("bmep").setMainField("materialId").setMainAlias("bpd").setJoinField("materialPlanId");
//            cq.createJoin(BusSupplier.class).setMainAlias("supplier").setMainField("vendorId").setMainAlias("bpd").setJoinField("supplierId");
//            cq.createJoin(SysDictData.class).setMainAlias("dictdata").setMainField("documentType").setMainAlias("bpd").setJoinField("dicDataValue");
//            cq.createJoin(BusMaterial.class).setMainAlias("material").setMainField("materialId").setMainAlias("bmep").setJoinField("materialId");
        }
        cq.createJoin(BusProject.class).setJoinField("projectId").setMainField("projectId");
        cq.createJoin(BaseUser.class).setMainField("userId").setJoinField("userId");
        cq.eq("bpd.purchaseId",requestMap.get("purchaseId"));
        cq.orderByDesc("purchase.createTime");
//        cq.eq("user.userId", OpenHelper.getUserId());
        cq.eq("project.projectId", requestMap.get("projectId"));
        return ResultBody.ok();
    }

    @Override
    public List<EntityMap> diclist(String code) {
        List<EntityMap> diclist=sysDictService.selectByCode(code);
         return diclist;
    }
}
